﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CaiBaby.Common;
using CaiBaby.Entity;

namespace CaiBaby.DAL
{
	/// <summary>
    /// 管理员权限访问层
	/// </summary>
	public class MangerPower
	{
		/// <summary>
        /// 增加一个权限
		/// </summary>
        /// <param name="mangerPowerInfo">权限实体</param>
        /// <returns>返回权限ID</returns>
        public int Add(MangerPowerInfo mangerPowerInfo)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("INSERT INTO ma_MangerPower(");
            strSql.Append("CId,Pname)");
			strSql.Append(" VALUES (");
            strSql.Append("@CId,@Pname)");
            strSql.Append(";SELECT @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@CId", SqlDbType.Int,4),
					new SqlParameter("@Pname", SqlDbType.VarChar,32)};
            parameters[0].Value = mangerPowerInfo.PId;
            parameters[1].Value = mangerPowerInfo.Pname;

            object obj = SQLHelper.GetSingle(ConfigHelper.CaiBabyDBConnStr, CommandType.Text, strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
		}

        /// <summary>
        /// 修改一个权限
        /// </summary>
        /// <param name="mangerPowerInfo">权限实体</param>
        /// <returns>返回是否成功</returns>
        public bool Update(MangerPowerInfo mangerPowerInfo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE ma_MangerPower SET ");
            strSql.Append("CId=@CId,Pname=@Pname) ");
            strSql.Append("WHERE PId=@PId ");
            SqlParameter[] parameters = {
                    new SqlParameter("@PId", SqlDbType.Int,4),
					new SqlParameter("@CId", SqlDbType.Int,4),
					new SqlParameter("@Pname", SqlDbType.VarChar,32)};
            parameters[0].Value = mangerPowerInfo.PId;
            parameters[1].Value = mangerPowerInfo.CId;
            parameters[2].Value = mangerPowerInfo.Pname;

            int rows = SQLHelper.ExecuteSql(ConfigHelper.CaiBabyDBConnStr,CommandType.Text,strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

		/// <summary>
        /// 删除一个权限
		/// </summary>
        /// <param name="pId">权限ID</param>
        /// <returns>返回是否成功</returns>
		public bool Delete(int pId)
		{
            string strSql1 = "DELETE FROM ma_MangerPower WHERE PId=@PId";
            string strSql2 = "DELETE FROM ma_RolePowerRelation WHERE PId=@PId";
			SqlParameter[] parameters = {
					new SqlParameter("@PId", SqlDbType.Int,4)};
            parameters[0].Value = pId;
            using (SqlTransaction tran = SQLHelper.BeginTransaction(ConfigHelper.CaiBabyDBConnStr))
            {
                try
                {
                    SQLHelper.ExecuteSql(tran, CommandType.Text, strSql1, parameters);
                    SQLHelper.ExecuteSql(tran, CommandType.Text, strSql2, parameters);
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    return false;
                }
            }
            return true;
		}
	}
}

